package com.wd.mylibrary_my.view.fragment

import android.content.Intent
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import coil.load
import coil.transform.RoundedCornersTransformation
import com.alibaba.android.arouter.launcher.ARouter
import com.wd.mylibrary_base.base.BaseFragment
import com.wd.mylibrary_base.utils.MyApp
import com.wd.mylibrary_my.R
import com.wd.mylibrary_my.view.activity.ImageInfoActivity
import com.wd.mylibrary_my.databinding.FragmentMyBinding
import com.wd.mylibrary_my.view.activity.ChangeNameActivity
import com.wd.mylibrary_my.view.activity.ChangeSignatureActivity
import com.wd.mylibrary_my.viewmodel.MyViewModel
import java.io.File


/**
 * A simple [Fragment] subclass.
 * Use the [MyFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class MyFragment : BaseFragment<MyViewModel,FragmentMyBinding>(R.layout.fragment_my) {
    override fun initView() {
        var userId = MyApp.sp.getInt("userId", 0)
        var sessionId = MyApp.sp.getString("sessionId","").toString()

        viewModel.getMyMessageData(userId,sessionId)
    }

    override fun dataObserve() {
        viewModel.myMessageData.observe(this,{
            if(it.status=="0000"){
                dataBinding.imgTou.load(it.result.headPic)
                dataBinding.name.text=it.result.nickName
                if(it.result.sex==1){
                    dataBinding.years.text="男"
                }else{
                    dataBinding.years.text="女"
                }
                dataBinding.fen.text=it.result.fanSum.toString()+" 粉丝"
                dataBinding.guan.text=it.result.workSum.toString()+" 作品数量"
                dataBinding.signature.text=it.result.signature
            }
        })
    }

    override fun onResume() {
        super.onResume()

        var userId = MyApp.sp.getInt("userId", 0)
        var sessionId = MyApp.sp.getString("sessionId","").toString()
        var nickName = MyApp.sp.getString("nickName", "")
        var signature = MyApp.sp.getString("signature", "")

        viewModel.getMyMessageData(userId,sessionId)

        //更换头像
        dataBinding.imgTou.setOnClickListener {
            if(dataBinding.name.text=="未登录" || dataBinding.name.text=="名字"){
                ARouter.getInstance().build("/login/Deng").navigation()
            }else{
                startActivity(Intent(context, ImageInfoActivity::class.java))
            }
        }

        //带数据 跳转 昵称
        dataBinding.name.setOnClickListener {
            if(dataBinding.name.text=="未登录" || dataBinding.name.text=="名字"){
                ARouter.getInstance().build("/login/Deng").navigation()
            }else{
                startActivity(Intent(context,ChangeNameActivity::class.java).apply {
                    putExtra("nickName",nickName)
                })
            }

        }

        //带数据 跳转 签名
        dataBinding.signature.setOnClickListener {
            if(nickName.equals("")){
                ARouter.getInstance().build("/login/Deng").navigation()
            }else {
                startActivity(Intent(context, ChangeSignatureActivity::class.java).apply {
                    putExtra("signature", signature)
                })
            }
        }

        //退出登录
        dataBinding.tui.setOnClickListener{
            MyApp.sp.edit().clear().commit()
            dataBinding.name.setText("未登录")
            dataBinding.imgTou.load(R.mipmap.ic_launcher)
            dataBinding.signature.visibility=View.GONE
        }
    }
}